import sys
from PIL import Image
import os
from tqdm import tqdm

def checkDirForDeepIsExists(dir_name):
    if not os.path.exists(dir_name):  # os模块判断并创建
        deep_dir_name = os.path.dirname(dir_name)
        checkDirForDeepIsExists(deep_dir_name)
        os.mkdir(dir_name)

imagePath="G:\数据集\植保所\分类目标图片\斑潜蝇危害状\分类后数据\重命名后230322"
savePath="G:\数据集\植保所\分类目标图片\斑潜蝇危害状\分类后数据\填充后230322"

for dirpath, dirnames, filenames in os.walk(imagePath):
    index=0
    for filename in tqdm(filenames):
        im = Image.open(os.path.join(dirpath,filename))
        x,y = im.size
        size=max(x,y)
        img=Image.new('RGB', (size, size), (255,255,255))
        img.paste(im, (int((size - x) / 2), int((size - y) / 2)))
        checkDirForDeepIsExists(dirpath.replace(imagePath, savePath))
        img.save(os.path.join(dirpath.replace(imagePath,savePath), filename))
